#ifndef SECTMANAGERPANEL_H
#define SECTMANAGERPANEL_H

#include "common.h"
#include <wx/dataview.h>
#include "nodemanager/wnodemanager.h"
#include "signal.hpp"

class SectManagerPanel;

class SectParamEditPanel : public wxPanel
{
public:
    SectParamEditPanel(wxWindow* parent);
    ~SectParamEditPanel();

    // 设置当前编辑区段
    inline void setSectNode(tagSectNode *pSectNode)
    {
        mSectNode=pSectNode;
    }
    // 得到当前编辑区段
    inline const tagSectNode* getSectNode(void) { return mSectNode; }
    // 挂接道岔改变信号槽
    void sig_ProcessItemChanged(SectManagerPanel *pSectManagerPanel);
    // 获取所有参数数据
    std::map<wxString, wxString> GetData() const;
    // 设置参数数据
    void SetData(const std::map<wxString, wxString>& data);
    // 设置参数范围
    inline void setRange(wxString range) { m_rangeText->SetValue(range); }
    // 得到参数范围
    inline wxString getRange(void) { return m_rangeText->GetValue(); }
    // 重置所有参数
    void resetAllData(void);

private:
    void InitUI();

    void OnSectNameTextChanged(wxCommandEvent& event);
    void OnSectNameTextKillFocus(wxFocusEvent& event);
    void OnSectNameTextEnter(wxCommandEvent& event);
    void OnDeviceIDTextChanged(wxCommandEvent& event);
    void OnDeviceIDTextKillFocus(wxFocusEvent& event);
    void OnDeviceIDTextEnter(wxCommandEvent& event);
    void OnLastSectChanged(wxCommandEvent& event);
    void OnLastSectKillFocus(wxFocusEvent& event);
    void OnLastSectEnter(wxCommandEvent& event);
    void OnNextSectChanged(wxCommandEvent& event);
    void OnNextSectKillFocus(wxFocusEvent& event);
    void OnNextSectEnter(wxCommandEvent& event);

    void OnLastSectComboBoxSelected(wxCommandEvent& event);
    void OnNextSectComboBoxSelected(wxCommandEvent& event);
    void OnAccessRuleComboBoxSelected(wxCommandEvent& event);

private:
    tagSectNode *mSectNode;
    sigslot::signal4<tagSectNode*,int,wxString,wxString> sig_item_change;
    std::map<wxString, wxString> m_lastChangedData;

    wxStaticText* m_sectNameLabel;
    wxTextCtrl* m_sectNameText;
    bool m_SectTextEditFinished;

    wxStaticText* m_deviceidLabel;
    wxTextCtrl* m_deviceidText;
    bool m_deviceidEditFinished;

    wxStaticText* m_rangeLabel;
    wxTextCtrl* m_rangeText;
    bool m_RangeTextEditFinished;

    wxStaticText* m_lastsectLabel;
    wxComboBox* m_lastsectText;
    bool m_LastSectEditFinished;

    wxStaticText* m_nextsectLabel;
    wxComboBox* m_nextsectText;
    bool m_NextSectEditFinished;

    wxStaticText* m_AccessRuleLabel;
    wxComboBox* m_AccessRuleText;
    bool m_AccessRuleEditFinished;
};

class SectRangePanel : public wxPanel
{
public:
    SectRangePanel(wxWindow *parent);
    ~SectRangePanel();

    // 设置当前编辑区段
    inline void setSectNode(tagSectNode *pSectNode)
    {
        mSectNode=pSectNode;
    }
    // 得到当前编辑结点
    inline const tagSectNode* getSectNode(void) { return mSectNode; }
    // 挂接道岔改变信号槽
    void sig_ProcessRangeItemChanged(SectManagerPanel *pSectManagerPanel);
    void clearAllData(void);
    void addData(const wxVector<wxVariant> data);
    // 重置所有参数
    void resetAllData(void);
    // 重新加载所有的结点
    void reloadAllNodes(void);

private:
    void OnEditingDone(wxDataViewEvent& event);
    void OnRightClick(wxDataViewEvent& event);

    void OnDeleteRange(wxCommandEvent& event);
    void OnAddNewRange(wxCommandEvent& event);

private:
    tagSectNode *mSectNode;
    sigslot::signal2<wxString,wxString> sig_range_change;

    wxDataViewCtrl* m_dvc;
    wxDataViewListStore* m_store;
    wxDataViewColumn* m_choicescolumnStart,*m_choicescolumnEnd;
};

class SectManagerPanel : public sigslot::has_slots<>,
                         public wxPanel
{
public:
    SectManagerPanel(wxWindow *parent);
    ~SectManagerPanel();

    void addSect(const tagSectNode* sectnode);
    void addData(const wxVector<wxVariant> data);

    void slot_ProcessItemChanged(tagSectNode *pSectNode,int itemindex,wxString olddata,wxString newdata);
    void slot_ProcessItemRangeChanged(wxString oldparams,wxString newparams);

private:
    void OnSelectionChanged(wxDataViewEvent& event);
    void OnRightClick(wxDataViewEvent& event);

    void OnDeleteSect(wxCommandEvent& event);
    void OnAddNewSect(wxCommandEvent& event);

private:
    wxDataViewCtrl* m_dvc;
    wxDataViewListStore* m_store;

    SectParamEditPanel *m_SectParamEditPanel;
    SectRangePanel *m_SectRangePanel;
};

#endif // SECTMANAGERPANEL_H
